/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jay9downloader;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author Jonas
 */
public class Video {

    public enum Status { WAITING, DOWNLOADING, DONE }

    private DecimalFormat df;
    private SimpleDateFormat sdf;
    private String title;
    private String downloadedTo;
    private String url;
    private String timestampString;
    private int width;
    private int height;
    private int duration;
    private int downloadProgress;

    private DownloadProgressListener downloadProgressListener;

    private static Video currentVideo;
    private Status status;


    public Video(String title, String url, String timestampString) {
        this.df = new DecimalFormat("00");
        this.sdf = new SimpleDateFormat();
        this.title = title;
        this.url = url;
        this.timestampString = timestampString;
        this.downloadProgress = 0;
        this.status = Status.WAITING;
    }

    public int getDuration() {
        return duration;
    }

    public String getDurationAsString() {
        int hours = this.duration / 3600;
        int tempminutes = this.duration % 3600;
        int minutes = tempminutes / 60;
        int seconds = tempminutes % 60;

        return df.format(hours) + ":" + df.format(minutes) + ":" + df.format(seconds);
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getDimensions() {
        return getWidth() + "x" + getHeight();
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTimestampString() {
        return timestampString;
    }

    public String getTimestampAsDateFormattedString() {
        sdf.applyPattern(sdf.toLocalizedPattern());
        return sdf.format(Long.parseLong(this.timestampString));
    }

    public void setTimestampString(String timestampString) {
        this.timestampString = timestampString;
    }

    public boolean isNewerThan(Video video) {
        if(Long.parseLong(this.timestampString) > Long.parseLong(video.getTimestampString())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return title;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Video other = (Video) obj;
        if ((this.title == null) ? (other.title != null) : !this.title.equals(other.title)) {
            return false;
        }
        if ((this.url == null) ? (other.url != null) : !this.url.equals(other.url)) {
            return false;
        }
        if ((this.timestampString == null) ? (other.timestampString != null) : !this.timestampString.equals(other.timestampString)) {
            return false;
        }
        if (this.width != other.width) {
            return false;
        }
        if (this.height != other.height) {
            return false;
        }
        if (this.duration != other.duration) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + (this.title != null ? this.title.hashCode() : 0);
        hash = 97 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 97 * hash + (this.timestampString != null ? this.timestampString.hashCode() : 0);
        hash = 97 * hash + this.width;
        hash = 97 * hash + this.height;
        hash = 97 * hash + this.duration;
        return hash;
    }

    public static Video getCurrentVideo() {
        return currentVideo;
    }

    public static void setCurrentVideo(Video currentVideo) {
        Video.currentVideo = currentVideo;
    }

    public static void doCurrentVideoChanged(Video newVideo, VideoListener vl) {
        Video.setCurrentVideo(newVideo);
        if(vl != null) {
            vl.updateVideoFields();
        }
    }

    public int getDownloadProgress() {
        return downloadProgress;
    }

    public void setDownloadProgress(int downloadProgress) {
        this.downloadProgress = downloadProgress;
        if(this.downloadProgressListener != null) {
            downloadProgressListener.updateDownloadProgress(this);
        }
    }

    public DownloadProgressListener getDownloadProgressListener() {
        return downloadProgressListener;
    }

    public void setDownloadProgressListener(DownloadProgressListener downloadProgressListener) {
        this.downloadProgressListener = downloadProgressListener;
    }

    public void setStatus(Status status) {
        this.status = status;
        if(this.status == Status.DONE) {
            this.setDownloadProgress(100);
        }
        if(this.downloadProgressListener != null) {
            downloadProgressListener.updateDownloadProgress(this);
        }
    }

    public Status getStatus() {
        return this.status;
    }

    public String getStatusAsString() {
        Status statusSwitch = this.status;
        switch(statusSwitch) {
            case WAITING    : return "Waiting";
            case DOWNLOADING: return "Downloading";
            case DONE       : return "Done";
            default         : return "Waiting";
        }
    }

    public String getDownloadedTo() {
        return downloadedTo;
    }

    public void setDownloadedTo(String downloadedTo) {
        this.downloadedTo = downloadedTo;
    }

}
